﻿using NetCore8Demo.Tool;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetCore8Demo.Models.Communication.SerialPortFolder;
using NetCore8Demo.ViewModels.Communication.SerialPortFolder;
using NetCore8Demo.BusinessData;
using HslCommunication;
using NetCore8Demo.Models.PLC;

namespace NetCore8Demo
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        CommonTool _commonTool = new CommonTool();

        bool _threadFlag = true;

        private void MainForm_Load(object sender, EventArgs e)
        {
            _commonTool.SafeLoad();
           // StaticTool.PlcToolList[0].ConnPlc();
           // StartThread();
        }

        private void LogoLabel_DoubleClick(object sender, EventArgs e)
        {
            AdminTestForm frm = new AdminTestForm();
            frm.Show();
        }



        Thread _readPlcThread = null;
        public void StartThread()
        {

            PlcInfo plcInfo = new PlcInfo();

            Thread _readPlcThread = new Thread(ReadPLCAction);
            _readPlcThread.Start(StaticPlc.PlcInfoList[0]);

           
        }

        public void ReadPLCAction(object? plcInfo)
        {
            while (_threadFlag)
            {
                Thread.Sleep(10);
                if(!(plcInfo is PlcInfo))
                {
                    continue;
                }

                PlcInfo plc = (PlcInfo)plcInfo;

                if (!plc.IsConnected)
                {
                    continue;
                }

               // StaticTool.ReadAll();
                StaticTool.PlcToolList[0].ReadAll();

            }
        }

        private void TestBtn_Click(object sender, EventArgs e)
        {
            StaticTool.PlcToolList[0].ConnPlc();
            StaticTool.PlcToolList[0].ReadAll();
        }

        
    }
}
